package com.web.dgfight.client;



import java.util.LinkedList;
import java.util.Queue;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.PushButton;



/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class DGFightWeb implements EntryPoint {
	/**
	 * This is the entry point method.
	 */
	
	private static final int REFRESH_INTERVAL = 2000; // ms
	public static int currentHealth = 100;
	public final static Label lblHealthScore = new Label(Integer.toString(currentHealth));
	public final static Label lblAttackScore = new Label("Attack Score");
	public final static Grid attackListGrid = new Grid(5, 1);
	static Queue<Integer> attackQueue = new LinkedList<Integer>();
	
	
	public final static Label lblTestText = new Label("New label");
	
	public void onModuleLoad() {
		
		
		// Use RootPanel.get() to get the entire body element
		RootPanel rootPanel = RootPanel.get("nameFieldContainer");
		rootPanel.setSize("650", "500");
		
		PushButton btnWeakAttack = new PushButton("New button");
		btnWeakAttack.setText("Weak Attack");
		rootPanel.add(btnWeakAttack);
		btnWeakAttack.setSize("100px", "28px");
		
		PushButton btnMidAttack = new PushButton("New button");
		btnMidAttack.setText("Mid Attack");
		rootPanel.add(btnMidAttack);
		btnMidAttack.setSize("100px", "28px");
		
		PushButton btnStrongAttack = new PushButton("New button");
		btnStrongAttack.setText("Strong Attack");
		rootPanel.add(btnStrongAttack);
		btnStrongAttack.setSize("100px", "28px");
		
		Label lblHealth = new Label("Health");
		rootPanel.add(lblHealth);
		rootPanel.add(lblHealthScore);
		
		final Label lblAttack = new Label("Attack");
		rootPanel.add(lblAttack);
		lblAttackScore.setStyleName("gwt-Label-AttackScore");
		
		rootPanel.add(lblAttackScore);
		rootPanel.add(attackListGrid);
		
		
		rootPanel.add(lblTestText);
		

		Timer refreshTimer = new Timer() {
		      @Override
		      public void run() {
		    	  		    	  
		    	  int currentAttack;
		    	  
		    	  if (currentHealth >0){
		    	  
		    		  if (attackQueue.peek() == null){
		    			  currentAttack = 0;
		    		  }
		    		  else {
		    			  currentAttack = attackQueue.poll(); // get the head value from the attack queue
		    		  }
		    		  //to stop the timer looping with null values but ready of attacks are added
		    		  lblHealthScore.setText(Integer.toString(currentHealth));
		    		  lblAttackScore.setText(Integer.toString(currentAttack)); 	    		  
		    		  //moves the attack score up the list
		    		  final Integer[] attackQueueArray = attackQueue.toArray(new Integer[0]);
		    		  for (int i = 0; i < attackQueue.size(); i++){
		    			  attackListGrid.setText(i, 0, attackQueueArray[i].toString());
		    		  }
		    	  
		    		  int hRValue = (100 - currentHealth)/5;
		    		  currentHealth = currentHealth - currentAttack + hRValue; // calculate the new current health
		    		  lblTestText.setText(Integer.toString(hRValue));
		    		  for (int i = attackQueue.size(); i < 4; i++){
		    			  attackListGrid.setText(i, 0, " ");
		    		  }
		    	  }
		    	  else{lblHealthScore.setText("Winner");}
		      }
		};
		    refreshTimer.scheduleRepeating(REFRESH_INTERVAL);
		    
		    // Listen for mouse events on the Add button.
		    btnWeakAttack.addClickHandler(new ClickHandler() {
		    	public void onClick(ClickEvent event) {
		    		attackRegister(5);
		    	}
		    	
		    });
		    btnMidAttack.addClickHandler(new ClickHandler() {
			      public void onClick(ClickEvent event) {
			    	  attackRegister(15);
			      }
			});
		    btnStrongAttack.addClickHandler(new ClickHandler() {
		    	public void onClick(ClickEvent event) {
		    		attackRegister(25);		    		
		    	}
		    });
		    
		     
	}
	
	// method for the button press to Register an attack to the attack queue	
	private void attackRegister (int attackStrength){
		int attackQueueSize = attackQueue.size();
		int aSResult = Randomise(attackStrength); 
		int hitProbability = (aSResult*aSResult/20)+(currentHealth/6);
				
		if (attackQueueSize <3){
			if ((Math.random()*100) < hitProbability) { 
				attackQueue.add(0);
				attackListGrid.setText(attackQueueSize, 0, "miss " + Integer.toString(hitProbability));
			}
			else {
			attackQueue.add(aSResult);
			attackListGrid.setText(attackQueueSize, 0, Integer.toString(aSResult));
			}
		}
	}
	// Randomizer method +/- 25%
	private int Randomise (int ScoreToRandomise){
		double STR = ScoreToRandomise; 
		int STRInt =  (int) (STR * (Math.random()/2 +0.75));	
		return STRInt;
	}	
}
